/*
 * Reflector application
 * A simple file filter class
 * Luke Mitchell 09-04-12 <lm0466@my.bristol.ac.uk>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;
import java.util.ArrayList;

class SimpleFileFilter extends javax.swing.filechooser.FileFilter
{
    private String description = "Files";
    private ArrayList<String> extensions;

    private String[] getExtensions ()
    {
        String[] array = new String[extensions.size ()];
        array = extensions.toArray (array);
        return array;
    }

    public void addExtension (String extension)
    {
        String lowerCaseExt = extension.toLowerCase ();
        if (!extensions.contains (lowerCaseExt))
            extensions.add (lowerCaseExt);
    }

    public boolean accept (File file)
    {
        if (file.isDirectory ())
        {
            return true;
        }

        String[] extensions = getExtensions ();

        for (String extension : extensions)
        {
            if (file.getName().toLowerCase().endsWith(extension))
            {
                return true;
            }
        } 
    
        return false;
    }

    public void setDescription (String description)
    {
        this.description = description;
    }

    public String getDescription ()
    {
        return description;
    }

    public SimpleFileFilter ()
    {
        extensions = new ArrayList<String> ();
    }
}
